home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  11.5 KB  |  381 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. static APTR memmoretext0,memmoretext1,memmoretext2,memmoretext3,memmorelist;
  33.  
  34. static APTR MemPool = NULL;
  35.  
  36. __asm __saveds LONG memlist_dspfunc(register __a2 char **array, register __a1 struct MemoEntry *mementry, register __a0 struct Hook *hook)
  37. {
  38.    if (mementry) {
  39.       *array++ = mementry->mem_address;
  40.       *array++ = mementry->mem_name;
  41.       *array++ = mementry->mem_type;
  42.       *array++ = mementry->mem_pri;
  43.       *array++ = mementry->mem_lower;
  44.       *array++ = mementry->mem_upper;
  45.       *array++ = mementry->mem_attributes;
  46.       *array   = NULL;
  47.    } else {
  48.       *array++ = ESC "bAddress";
  49.       *array++ = ESC "bln_Name";
  50.       *array++ = ESC "bln_Type";
  51.       *array++ = ESC "bln_Pri";
  52.       *array++ = ESC "bmh_Lower";
  53.       *array++ = ESC "bmh_Upper";
  54.       *array++ = ESC "bmh_Attr";
  55.       *array = NULL;
  56.    }
  57.    return(0);
  58. }
  59.  
  60. struct Hook memlist_dsphook = {
  61.  {NULL, NULL},
  62.  (ULONG (* )())memlist_dspfunc,
  63.  NULL, NULL
  64. };
  65.  
  66. void FreeMemory (void)
  67. {
  68.     MyFreePoolStructs(&MemPool, memtext, NULL, memlist);
  69. }
  70.  
  71. int GetMemory (struct MemoEntry **first) {
  72.    struct   MemHeader      *mem;
  73.    struct   MemoEntry      *mementry,*previous = NULL;
  74.  
  75.    int memcnt = 0;
  76.    *first = 0;
  77.  
  78.    mem = FIRSTMEMORY;
  79.  
  80.    if (!MemPool) MemPool = tbCreatePool(MEMF_CLEAR, 4096, 4096);
  81.  
  82.    if (clientstate) {
  83.       if (SendDaemon ("GetMemList")) {
  84.          while ((mementry = tbAllocPooled(MemPool, sizeof(struct MemoEntry))) \
  85.            && (ReceiveDecodedEntry ((UBYTE *) mementry, sizeof (struct MemoEntry)))) {
  86.             IsHex (mementry->mem_address, (long *) &mementry->mem_adr);
  87.  
  88.             if (! *first)
  89.                *first = mementry;
  90.             if (previous)
  91.                previous->mem_next = mementry;
  92.  
  93.             memcnt++;
  94.             previous = mementry;
  95.          }
  96.       }
  97.    } else {
  98.       while ((mem->mh_Node.ln_Succ != 0) && (mementry = tbAllocPooled(MemPool, sizeof(struct MemoEntry)))) {
  99.          if (! *first)
  100.             *first = mementry;
  101.          if (previous)
  102.             previous->mem_next = mementry;
  103.  
  104.          mementry->mem_adr = mem;
  105.  
  106.          _sprintf (mementry->mem_address, "$%08lx", mem);
  107.          strncpy (mementry->mem_name, nonetest (mem->mh_Node.ln_Name), 20);
  108.          strcpy (mementry->mem_type, GetNodeType (mem->mh_Node.ln_Type));
  109.          _sprintf (mementry->mem_pri, "%4ld ", mem->mh_Node.ln_Pri);
  110.          _sprintf (mementry->mem_lower, "$%08lx", mem->mh_Lower);
  111.          _sprintf (mementry->mem_upper, "$%08lx", mem->mh_Upper);
  112.          _sprintf (mementry->mem_attributes, " $%04lx", mem->mh_Attributes);
  113.          memcnt++;
  114.  
  115.          previous = mementry;
  116.          mem = (struct MemHeader *) mem->mh_Node.ln_Succ;
  117.       }
  118.    }
  119.    return (memcnt);
  120. }
  121.  
  122. void PrintMemory (char *filename) {
  123.    int   i=1;
  124.    BPTR  handle;
  125.    struct MemoEntry *entryp = NULL;
  126.  
  127.    handle = HandlePrintStart (filename);
  128.    if ((handle) && (PrintOneLine (handle, "\n  Address  Name                 Type     Pri    Lower     Upper     Attr\n\n"))) {
  129.       if (! WI_Memory) {
  130.          i = GetMemory (&entryp);
  131.       }
  132.       if (i) {
  133.          for (i=0;;i++) {
  134.             if (WI_Memory)
  135.                DoMethod (memlist,MUIM_List_GetEntry,i,&entryp);
  136.             if (!entryp) break;
  137.  
  138.             _sprintf (tmpstr2, " %s %-20.20s %-8s%4s %s %s %s\n", entryp->mem_address, entryp->mem_name, entryp->mem_type, entryp->mem_pri, entryp->mem_lower, entryp->mem_upper, entryp->mem_attributes);
  139.             if (! (PrintOneLine (handle, tmpstr2)))
  140.                break;
  141.  
  142.             if (! WI_Memory)
  143.                entryp = entryp->mem_next;
  144.          }
  145.       }
  146.    }
  147.    HandlePrintStop();
  148. }
  149.  
  150. void ShowMemory (void) {
  151.    struct   MemoEntry      *memo;
  152.  
  153.    ApplicationSleep();
  154.    set (memlist,MUIA_List_Quiet,TRUE);
  155.    set (BT_MemPriority, MUIA_Disabled, TRUE);
  156.    set (BT_MemMore, MUIA_Disabled, TRUE);
  157.  
  158.    FreeMemory();
  159.    GetMemory (&memo);
  160.  
  161.    while (memo) {
  162.       InsertBottomEntry (memlist, (APTR *) &memo);
  163.       memo = memo->mem_next;
  164.    }
  165.  
  166.    AwakeApplication();
  167.    set (memlist,MUIA_List_Quiet,FALSE);
  168. }
  169.  
  170. void SendMemList (void) {
  171.    struct   MemoEntry      *memo;
  172.  
  173.    FreeMemory();
  174.    GetMemory (&memo);
  175.  
  176.    while (memo) {
  177.       SendEncodedEntry ((UBYTE *) memo, sizeof (struct MemoEntry));
  178.       memo = memo->mem_next;
  179.    }
  180.    FreeMemory();
  181. }
  182.  
  183. __asm __saveds LONG memmore_dspfunc(register __a2 char **array, register __a1 struct HunkEntry *hunkentry, register __a0 struct Hook *hook)
  184. {
  185.    if (hunkentry) {
  186.       *array++ = hunkentry->he_lower;
  187.       *array++ = hunkentry->he_upper;
  188.       *array++ = hunkentry->he_size;
  189.       *array   = NULL;
  190.    } else {
  191.       *array++ = ESC "bStart";
  192.       *array++ = ESC "bEnd";
  193.       *array++ = ESC "bSize";
  194.       *array   = NULL;
  195.    }
  196.    return (0);
  197. }
  198.  
  199. struct Hook memmore_dsphook = {
  200.  {NULL, NULL},
  201.  (ULONG (* )())memmore_dspfunc,
  202.  NULL, NULL
  203. };
  204.  
  205.  
  206. void GetChunkList (struct MemHeader *mem) {
  207.    #define mementryanzahl 20
  208.  
  209.    struct   HunkEntry  *hunkentry,*chunkmem;
  210.    struct   MemChunk   *first,*chunk;
  211.    ULONG    size;
  212.    int      count = 0;
  213.  
  214.    set(memmorelist,MUIA_List_Quiet,TRUE);
  215.  
  216.    if (first = mem->mh_First) {
  217.  
  218.       chunk = first;
  219.       while (chunk) {
  220.          count++;
  221.          chunk = chunk->mc_Next;
  222.       }
  223.       count += 10;
  224.  
  225.       if (chunkmem = tbAllocPooled(MemPool, count * sizeof(struct HunkEntry))) {
  226.  
  227.          chunk = first;
  228.          hunkentry = chunkmem;
  229.          while ((count) && (chunk)) {
  230.             size = chunk->mc_Bytes;
  231.             _sprintf (hunkentry->he_lower, "$%08lx", chunk);
  232.             _sprintf (hunkentry->he_size, "%8ld", size);
  233.             _sprintf (hunkentry->he_upper, "$%08lx", ((char *) chunk) + size);
  234.  
  235.             InsertBottomEntry (memmorelist, (APTR *) &hunkentry);
  236.  
  237.             hunkentry += 1;
  238.             chunk = chunk->mc_Next;
  239.             count--;
  240.          }
  241.       }
  242.    }
  243.    set(memmorelist,MUIA_List_Quiet,FALSE);
  244. }
  245.  
  246. void GetMemTypeToTmpstr (ULONG attributes) {
  247.    int   i=0;
  248.  
  249.    ULONG memtype[]  = { MEMF_PUBLIC,MEMF_CHIP,MEMF_FAST,
  250.                         MEMF_LOCAL,MEMF_24BITDMA,MEMF_KICK,
  251.                         MEMF_CLEAR,MEMF_LARGEST,MEMF_REVERSE,MEMF_TOTAL,
  252.                         MEMF_ANY };
  253.  
  254.    char  *memtext[] = { "PUBLIC","CHIP","FAST",
  255.                         "LOCAL","24BITDMA","KICK",
  256.                         "CLEAR","LARGEST","REVERSE","TOTAL" };
  257.  
  258.    tmpstr[0] = '\0';
  259.    while (memtype[i]) {
  260.       if (attributes & memtype[i]) {
  261.          if (strlen (tmpstr))
  262.             strcat (tmpstr, ", ");
  263.          strcat (tmpstr, memtext[i]);
  264.       }
  265.       i++;
  266.    }
  267.    if (!attributes)
  268.       strcpy (tmpstr, "ANY");
  269. }
  270.  
  271. void GetMemMore (struct MemHeader *mem) {
  272.    int   total, inuse;
  273.  
  274.    unsigned char     *title = "MEMORY: ";
  275.    struct   WinFree  *ptr;
  276.  
  277.    if (ptr = AllocWinFree()) {
  278.       ptr->wf_Window = (APTR) WindowObject,
  279.  
  280. //      MUIA_Window_SizeGadget, FALSE,
  281.       MUIA_HelpNode, MemoryText,
  282.       MUIA_Window_ID, MakeDetailID('.','M','E','M'),
  283.       WindowContents, HGroup,
  284.          Child, VGroup, MUIA_Group_SameWidth, TRUE,
  285.             Child, MyLabel2 ("Name:"),
  286.             Child, MyLabel2 ("Address:\nPri:\nType:\nFree:\nLargest:"),
  287.             Child, MyLabel2 ("Attributes:"),
  288.             Child, MyLabel2 ("\nChunks:"),
  289.             Child, MyVSpace(0),
  290.          End,
  291.          Child, VGroup, MUIA_Group_SameWidth, TRUE,
  292.             Child, memmoretext0 = MyTextObject(),
  293.             Child, HGroup,
  294.                Child, memmoretext1 = MyTextObject2(),
  295.                Child, MyLabel ("First:\nLower:\nUpper:\nInUse:\nTotal:"),
  296.                Child, memmoretext2 = MyTextObject2(),
  297.             End,
  298.             Child, memmoretext3 = MyTextObject(),
  299.             Child, memmorelist = ListviewObject,
  300.                MUIA_Listview_DoubleClick, TRUE,
  301.                MUIA_Listview_Input, FALSE,
  302.                MUIA_Listview_List, ListObject,
  303.                MUIA_List_Format, "COL=0 DELTA=8,COL=1 DELTA=8,COL=2 P=\33r",
  304. //               MUIA_List_AdjustWidth, TRUE,
  305.                MUIA_List_Title, TRUE,
  306.                MUIA_List_DisplayHook, &memmore_dsphook,
  307.                ReadListFrame,
  308.                End,
  309.             MUIA_CycleChain, TRUE,
  310.             End,
  311.          End,
  312.       End, End;
  313.  
  314.       total = ((int) mem->mh_Upper) - ((int) mem->mh_Lower);
  315.       inuse = total - ((int) mem->mh_Free);
  316.  
  317.       if (ptr->wf_Window) {
  318.          GetChunkList(mem);
  319.  
  320.          MySetContents (memmoretext1, ESC "r$%08lx\n" ESC "c%ld\n%s\n" ESC "r%ld\n%ld", mem, mem->mh_Node.ln_Pri, GetNodeType (mem->mh_Node.ln_Type), mem->mh_Free, AvailMem (MEMF_LARGEST | mem->mh_Attributes));
  321.          MySetContents (memmoretext2, ESC "r$%08lx\n$%08lx\n$%08lx\n%ld\n%ld", mem->mh_First, mem->mh_Lower, mem->mh_Upper, inuse, total);
  322.  
  323.          GetMemTypeToTmpstr ((ULONG) mem->mh_Attributes);
  324.          set (memmoretext3, MUIA_Text_Contents, tmpstr);
  325.  
  326.          HandleWindowOpen (ptr, title, nonetest (mem->mh_Node.ln_Name));
  327.          set (ptr->wf_Window,MUIA_Window_ActiveObject,memmorelist);
  328.          set (memmoretext0, MUIA_Text_Contents, nonetest (mem->mh_Node.ln_Name));
  329.          HandleWindowClose (ptr);
  330.       }
  331.    }
  332. }
  333.  
  334.  
  335. char memory_title[WINDOWTITLELEN];
  336.  
  337. void MemoryWindow (BOOL state) {
  338.    if (state) {
  339.       if (WI_Memory) {
  340.          ShowMemory();
  341.       } else {
  342.          WI_Memory = WindowObject,
  343.          MUIA_Window_Title, MyGetWindowTitle (memory_title, "MEMORY"),
  344.          MUIA_HelpNode, MemoryText,
  345.          MUIA_Window_ID, MakeListID('M','E','M','O'),
  346.          WindowContents, VGroup,
  347.             Child, memlist = MyListviewObject ("COL=0 DELTA=8,COL=1 DELTA=8,COL=2 DELTA=8,COL=3 DELTA=8 P=\33r,COL=4 DELTA=8,COL=5 DELTA=8,COL=6",&memlist_dsphook),
  348.             Child, memtext = MyTextObject(),
  349.             Child, MyVSpace(2),
  350.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  351.                Child, BT_MemPrint    = KeyButtonA (PrintText   ,ID_MEMPRINT),
  352.                Child, BT_MemPriority = KeyButtonA (PriorityText,ID_MEMPRIORITY),
  353.                Child, BT_MemMore     = KeyButtonA (MoreText    ,ID_MEMMORE),
  354.                Child, BT_MemExit     = KeyButtonA (ExitText    ,ID_MEMEXIT),
  355.             End,
  356.          End, End;
  357.  
  358.          DoMethod (AP_Scout,OM_ADDMEMBER,WI_Memory);
  359.          DoMethod (WI_Memory,MUIM_Window_SetCycleChain,memlist,BT_MemPrint,BT_MemPriority,BT_MemMore,BT_MemExit,NULL);
  360.  
  361.          SetCloseRequest (WI_Memory,ID_MEMEXIT);
  362.          SetListActive (memlist,ID_MEMLV_ACTIVE);
  363.          SetListviewDoubleClick (memlist,ID_MEMMORE);
  364.  
  365.          ShowMemory();
  366.  
  367.          SetWindowOpen (WI_Memory,memlist,ID_MEMEXIT);
  368.       }
  369.    } else if ((! state) && (WI_Memory)) {
  370.       SetWindowClose (WI_Memory,TRUE);
  371.  
  372.       FreeMemory();
  373.  
  374.       DoMethod (AP_Scout,OM_REMMEMBER,WI_Memory);
  375.       MUI_DisposeObject (WI_Memory);
  376.       WI_Memory = NULL;
  377.       memlist = NULL;
  378.    }
  379. }
  380.  
  381.